package gui;

import database.*;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

public class PanelEndreRett extends JPanel implements ActionListener {
	private JScrollPane scrollPane;
	private DefaultTableModel tableModel;
	private JTable tabell;
	private JButton  buttonVelg, buttonHovedmeny, buttonEndre, buttonTilbake;
	private Vector<String> kolonnenavn;
	private GridBagConstraints constraints;
	private JLabel labelEndreRett, labelNavn,labelBeskrivelse,labelPris;
	private JTextField fieldNavn,fieldBeskrivelse,fieldPris;
	private List<Rett> retter;
	private Rett rett;
	private int[] valgt;
	
	public PanelEndreRett(){
		this.setLayout(new GridBagLayout());
		this.leggTilKomponenter();
		GUI.getVindu().add(this);
		
	}
	private void leggTilKomponenter(){
		kolonnenavn = getKolonnenavn();
		tableModel = new DefaultTableModel(kolonnenavn,0){
			@Override
			public boolean isCellEditable(int row, int column) {
			       //all cells false
			       return false;
			}
		};
		fyllTableModel();
		
		buttonVelg = new JButton("Velg");
		buttonVelg.setEnabled(false);
		buttonVelg.addActionListener(this);
		
		labelEndreRett = new JLabel("Endre rett");
		
		labelNavn = new JLabel("Navn");
		labelBeskrivelse = new JLabel("Beskrivelse");
		labelPris = new JLabel("Pris");
		fieldNavn = new JTextField(20);
		fieldNavn.setEnabled(false);
		fieldBeskrivelse = new JTextField(20);
		fieldBeskrivelse.setEnabled(false);
		fieldPris = new JTextField(20);
		fieldPris.setEnabled(false);
		
		buttonEndre = new JButton("Endre");
		buttonEndre.addActionListener(this);
		buttonEndre.setEnabled(false);
		
		buttonHovedmeny = new JButton("Tilbake til hovedmeny");
		buttonHovedmeny.addActionListener(this);
		
		buttonTilbake = new JButton("Tilbake");
		buttonTilbake.addActionListener(this);
		
		tabell = new JTable(tableModel);
		tabell.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		ListSelectionModel rad = tabell.getSelectionModel();
		rad.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				radSelected(e);
				
			}
		});
		tabell.getColumnModel().getColumn(0).setPreferredWidth(50);
		tabell.getColumnModel().getColumn(1).setPreferredWidth(80);
		tabell.getColumnModel().getColumn(2).setPreferredWidth(240);
		tabell.getColumnModel().getColumn(3).setPreferredWidth(50);
		
		constraints = new GridBagConstraints();
		constraints.insets = new Insets(10, 10, 10, 10);
		
		Font f = new Font(labelEndreRett.getFont().getName(), labelEndreRett.getFont().getStyle(), 30);
		labelEndreRett.setFont(f);
	
		scrollPane = new JScrollPane(tabell);
		scrollPane.setMinimumSize(new Dimension(600, 400));
		scrollPane.setPreferredSize(new Dimension(600, 400));
		
		constraints.gridwidth = 2;
		this.add(labelEndreRett, constraints);
		
		constraints.weightx = 0.5;
		constraints.gridy = 1;
		constraints.gridx = 0;
		constraints.gridheight = 4;
		this.add(scrollPane,constraints);
		constraints.gridheight = 1;
		constraints.gridwidth = 1;
		constraints.gridx = 0;
		constraints.anchor = GridBagConstraints.LINE_END;
		constraints.gridy = 5;
		this.add(buttonTilbake,constraints);
		constraints.gridx = 1;
		constraints.anchor = GridBagConstraints.LINE_START;
		this.add(buttonHovedmeny,constraints);
		constraints.gridwidth = 1;
		constraints.weightx = 0;
		constraints(2, 1);
		constraints.anchor = GridBagConstraints.LINE_END;
		this.add(labelNavn, constraints);
		constraints.weightx = 1;
		constraints(3, 1);
		constraints.anchor = GridBagConstraints.LINE_START;
		this.add(fieldNavn, constraints);
		constraints.weightx = 0;
		constraints(2, 2);
		constraints.anchor = GridBagConstraints.LINE_END;
		this.add(labelBeskrivelse, constraints);
		constraints(3, 2);
		constraints.anchor = GridBagConstraints.LINE_START;
		this.add(fieldBeskrivelse, constraints);
		constraints(2, 3);
		constraints.anchor = GridBagConstraints.LINE_END;
		this.add(labelPris, constraints);
		constraints(3, 3);
		constraints.anchor = GridBagConstraints.LINE_START;
		this.add(fieldPris, constraints);
		constraints(3, 4);
		constraints.gridwidth = 1;
		constraints.anchor = GridBagConstraints.NORTHWEST;
		this.add(buttonEndre, constraints);
		
		
	}
	private void constraints(int x, int y){
		constraints.gridx = x;
		constraints.gridy = y;
	}
	private void radSelected(ListSelectionEvent e){
		
		if (tabell.getSelectedRows().length == 0) {
	        //No selection, disable fire button.

	        } else {
	        //Selection, enable the fire button.
	            fyllFelt();
	            buttonEndre.setEnabled(true);
	        }
	}
	private void fyllFelt() 
	{
		valgt = tabell.getSelectedRows();
		rett = retter.get(valgt[0]);
		fieldNavn.setText(rett.getNavn());
		fieldBeskrivelse.setText(rett.getBeskrivelse());
		fieldPris.setText(Integer.toString(rett.getPris()));
		fieldNavn.setEnabled(true);
		fieldBeskrivelse.setEnabled(true);
		fieldPris.setEnabled(true);
		
	}
	private void buttonTilbake_actionPerformed(ActionEvent e)
	{
		this.setVisible(false);
		new PanelMeny();
	}
	private void buttonHovedmeny_actionPerformed(ActionEvent e)
	{
		this.setVisible(false);
		new PanelHovedmeny();
	}

	private void buttonEndre_actionPerformed(ActionEvent e) {
		if(valider()){
			Databasecon.updateRettNavn(rett, fieldNavn.getText());
			Databasecon.updateRettBesk(rett, fieldBeskrivelse.getText());
			Databasecon.updateRettPris(rett, Integer.parseInt(fieldPris.getText()));
			tabell.setValueAt(fieldNavn.getText(), valgt[0], 1);
			tabell.setValueAt(fieldBeskrivelse.getText(), valgt[0], 2);
			tabell.setValueAt(fieldPris.getText(), valgt[0], 3);
		}
		
	}
	private boolean valider(){
		if(!Register.validerTekst(fieldNavn.getText())|| fieldNavn.getText().isEmpty()){
			JOptionPane.showMessageDialog(null, "Ugyldig navn");
			return false;
			
		}
		else if(!Register.validerTallOgBokstaverTillat(fieldBeskrivelse.getText())|| fieldBeskrivelse.getText().isEmpty()){
			JOptionPane.showMessageDialog(null, "Ugyldig beskrivelse");
			return false;
		}
		else if(!Register.validerPris(fieldPris.getText())||fieldPris.getText().isEmpty()){
			JOptionPane.showMessageDialog(null, "Ugyldig pris");
			return false;
		}
		else{
			return true;
		}
	}
	private Vector<String> getKolonnenavn(){
		Vector<String> kolonnenavn = new Vector<String>();
		kolonnenavn.add("Nummer");
		kolonnenavn.add("Navn");
		kolonnenavn.add("Beskrivelse");
		kolonnenavn.add("Pris");
		return kolonnenavn;
	}
	private void fyllTableModel(){
		retter = Databasecon.getMeny();
		for (int i = 0; i < retter.size(); i++) {
			Vector<String> radData = new Vector<String>();
			radData.add(Integer.toString(retter.get(i).getVareID()));
			radData.add(retter.get(i).getNavn());
			radData.add(retter.get(i).getBeskrivelse());
			radData.add(Integer.toString(retter.get(i).getPris()));
			tableModel.addRow(radData);
		}
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==buttonHovedmeny)
			buttonHovedmeny_actionPerformed(e);
		else if(e.getSource()==buttonEndre){
			buttonEndre_actionPerformed(e);
		}
		else if(e.getSource()==buttonTilbake)
			buttonTilbake_actionPerformed(e);
		
	}
}
